package com.jboost.modules.app.service.strategry.newxihui;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.JsonObject;
import com.jboost.common.util.DateUtils;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.*;
import com.jboost.modules.app.dto.newhuixin.PriceReq;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service(value = "XmXiHuiPriceJob")
@Slf4j
public class XmXiHuiPriceJob extends AbstractXmAllureUser<JsonObject, Boolean> {
    public String getAppId(){
        return "6800b7cb5178a4fbba6dc231";
    }

    @Async
    @Scheduled(cron = "0 10 12 * * ?")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        String s = "XmXiHuiPriceJob" + DateUtils.date_sdf.get().format(new Date());
        boolean b = redisUtil.hasKey(s);
        if (b) {
            return BaseResult.success();
        }
        Long no = (Long)redisUtil.get("XmXiHuiPriceJob" + DateUtils.date_sdf.get().format(DateUtil.yesterday()));
        if (Objects.isNull(no)) {
            no = 100010l;
        }
        ++no;
        Boolean add = add(no);
        if (add) {
            redisUtil.set(s, no, 864000);
        }
        return BaseResult.success();
    }





    public String getFormId() {
        return "65420eeee1311d5e328e5df4";
    }

    private Boolean add(Long no) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(lvPriceUrl).get()
                .build();
        Response execute = client.newCall(request).execute();
        JSONObject data = (JSONObject)JSONUtil.fromJson(execute.body().string(), Map.class).get("data");
        JSONObject jsonObject = data.getJSONObject("10");
        String price = "";
        Long time = 1L;
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        for (Map.Entry<String, Object> objectEntry:entries){
            JSONObject value = (JSONObject)objectEntry.getValue();
            if (Long.parseLong(objectEntry.getKey())>time){
                price = value.getString("max");
                time = Long.parseLong(objectEntry.getKey());
            }

        }
        if (StringUtils.isBlank(price)) {
            return false;
        }
        HashMap<String, Object> param = getParam();
        PriceReq req = new PriceReq();
        req.setValite(getParaMap("是"));
        req.setNo(getParaMap(no));
        req.setPrice(getParaMap(price));
        req.setTime(getParaMap(DateUtils.date_sdf.get().format(new Date())));
        param.put("data", req);
        String post = HttpRestUtils.post(add_api, param, jdyHeaders);
        HashMap hashMap = JSONUtil.fromJson(post, HashMap.class);
        log.info("表单新增结果{}", post);
        return true;
    }




}
